<template>
  <div>
    <el-card
      shadow="always"
      style="margin: 20px"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="rows"
      :current-page="pageNo"
      @current-change="currentChangeHandle"
    >
      <template #header>
        <div>
          <h2>Information</h2>
        </div></template
      >

      <el-table
        ref="multipleTable"
        v-loading="dataListLoading"
        :data="dataList"
      >
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          label="name"
        />
        <el-table-column
          prop="username"
          header-align="center"
          align="center"
          label="username"
        />
        <el-table-column
          prop="email"
          header-align="center"
          align="center"
          :label="$t('group.operator.email')"
        />
        <el-table-column
          prop="creTime"
          header-align="center"
          align="center"
          :label="$t('group.operator.creTime')"
        >
          <template v-slot="scope">
            <label>{{
              formatDate(scope.row.creTime, 'YYYY-MM-DD  HH:mm:ss')
            }}</label>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          :label="$t('common.operate')"
        >
          <template v-slot="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('operator.resetPassBtn')"
              placement="top"
            >
              <el-button
                v-if="isAuth('10400202.MANAGE')"
                icon="el-icon-refresh-left"
                @click="reset(scope.row.userId)"
              />
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('operator.updateEmail')"
              placement="top"
            >
              <el-button
                v-if="scope.row.status == 1"
                icon="el-icon-message"
                @click="updateEmail(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <update-email
      v-if="updateEmailVisible"
      ref="updateEmail"
      @close="updateEmailClose"
    />
  </div>
</template>

<script>
import { formatDate } from '@/utils/formatDate'
import { getaccountList, getaccountPage, resetPassword } from '@/api/operator'
import UpdateEmail from './update-email'
import { isAuth } from '@/utils'

export default {
  components: {
    UpdateEmail
  },
  data() {
    return {
      visible: false,
      comfirmLoading: false,
      operatorId: 0,
      operatorType: '',
      dataForm: {
        operatorName: '',
        appPkgName: '',
        pubKey: '',
        remark: ''
      },
      updateEmailVisible: false,
      dataList: [],
      dataListSelections: [],
      dataListLoading: false,
      pageNo: 1,
      pageSize: 10,
      rows: 0
    }
  },
  methods: {
    init(id, operatorType) {
      this.operatorType = operatorType
      this.operatorId = id
      this.visible = true
      this.dataList = []
      this.pageNo = 1
      this.getDataList()
    },
    cancel() {
      this.visible = false
      this.$emit('close')
    },
    updateEmail(row) {
      this.updateEmailVisible = true
      this.$nextTick(() => {
        this.$refs.updateEmail.init(row.userId)
      })
    },
    updateEmailClose() {
      this.updateEmailVisible = false
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNo = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      console.log(val)
      this.pageNo = val
      this.getDataList()
    },
    formatDate(cellValue, fmt) {
      cellValue = new Date(cellValue).valueOf()
      return formatDate(cellValue, fmt)
    },
    getDataList() {
      this.dataListLoading = true
      getaccountPage({
        characterCode: 'OPERATOR',
        keyId: this.operatorId,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      })
        .then((data) => {
          console.log(data)
          this.dataListLoading = false
          this.dataList = data.data
          this.rows = data.rows
        })
        .catch((e) => {
          this.$confirmError(e)
          this.loading = false
          this.dataListLoading = false
        })
    },
    handleClose() {
      this.visible = false
      this.comfirmLoading = false
    },
    reset(userId) {
      if (this.operatorType === 'DEMO') {
        console.log(this.operatorType)
        this.$confirm(this.$t('operator.resetDefaultConfirm'), '', {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }).then(() => {
          this.resetPasswordHanle(userId)
        })
      } else {
        this.$confirm(this.$t('operator.resetConfirm'), '', {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }).then(() => {
          this.resetPasswordHanle(userId)
        })
      }
    },
    resetPasswordHanle(userId) {
      resetPassword(userId)
        .then((data) => {
          this.$message({
            type: 'success',
            message: this.$t('common.success')
          })
        })
        .catch((err) => {
          this.$confirmError(err)
        })
    },
    isAuth
  }
}
</script>
